package com.i8office.android.zhihuixinwu.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.cyan.tools.base.BaseViewHolder;
import com.cyan.tools.base.RecyclerArrayAdapter;
import com.example.menhoo.toollibrary.common.utils.DisplayUtil;
import com.i8office.android.zhihuixinwu.R;
import com.i8office.android.zhihuixinwu.ui.schedule.AddEventActivity;
import com.i8office.android.zhihuixinwu.ui.schedule.AddLeaderEventActivity;
import com.i8office.android.zhihuixinwu.ui.schedule.EventItem;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by pc on 2018/7/8.
 */

public class ScheduleAdapter extends RecyclerArrayAdapter<EventItem> {
    private Context mContext;
    private OnOperateListener onOperateListener;

    public ScheduleAdapter(Context context, OnOperateListener onOperateListener) {
        super(context);
        this.mContext = context;
        this.onOperateListener = onOperateListener;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent);
    }

    class ViewHolder extends BaseViewHolder<EventItem> {
        @BindView(R.id.location)
        TextView location;
        @BindView(R.id.department)
        TextView department;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.content)
        View contentView;
        @BindView(R.id.delete)
        View delete;

        public ViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_event);
            ButterKnife.bind(this, itemView);
            setDrawableLeft(location, R.drawable.icon_location);
            setDrawableLeft(department, R.drawable.icon_department);
        }

        @Override
        public void setData(final EventItem eventItem) {

            location.setText(eventItem.getLocation());
            location.setVisibility(TextUtils.isEmpty(eventItem.getLocation()) ? View.GONE : View.VISIBLE);
            department.setText(eventItem.getDeptName());
            department.setVisibility(TextUtils.isEmpty(eventItem.getDeptName()) ? View.GONE : View.VISIBLE);
            title.setText(eventItem.getTitle());
            title.setTextColor(ContextCompat.getColor(mContext, eventItem.getCanEdit() == 0 ? R.color.textcolor3 : R.color.black));
            time.setText(TextUtils.isEmpty(eventItem.getStartTimeString()) ? "-- : --" : eventItem.getStartTimeString());
            contentView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onOperateListener != null) {
                        onOperateListener.onClickItem(eventItem);
                    }
                }
            });
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new MaterialDialog.Builder(mContext)
                            .content(R.string.delete_event)
                            .positiveText(R.string.confirm)
                            .onPositive(new MaterialDialog.SingleButtonCallback() {
                                @Override
                                public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                                    if (onOperateListener != null) {
                                        onOperateListener.onDelete(eventItem);
                                    }
                                }
                            })
                            .negativeText(R.string.cancel)
                            .show();
                }
            });
        }
    }

    private void setDrawableLeft(TextView view, @DrawableRes int drawableRes) {
        Drawable drawable = ContextCompat.getDrawable(mContext, drawableRes);
        drawable.setBounds(0, 0, DisplayUtil.dip2px(mContext, 12), DisplayUtil.dip2px(mContext, 12));
        view.setCompoundDrawables(drawable, null, null, null);
    }

    public interface OnOperateListener {
        void onDelete(EventItem eventItem);

        void onClickItem(EventItem eventItem);
    }

}
